﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simpletron
{
  public class InstructionMap
  {
    private Dictionary<int, Instruction> map;

    public static InstructionMap Instance { get; private set; }

    static InstructionMap()
    {
      Instance = new InstructionMap();
    }

    private InstructionMap()
    {
      map = new Dictionary<int, Instruction>();
      map.Add(10, InstructionFactory.ReadInstruction());
      map.Add(11, InstructionFactory.WriteInstruction());
      map.Add(20, InstructionFactory.LoadInstruction());
      map.Add(21, InstructionFactory.StoreInstruction());
      map.Add(30, InstructionFactory.AddInstruction());
      map.Add(31, InstructionFactory.SubtractInstruction());
      map.Add(32, InstructionFactory.DivideInstruction());
      map.Add(33, InstructionFactory.MultiplyInstruction());
      map.Add(40, InstructionFactory.BranchInstruction());
      map.Add(41, InstructionFactory.BranchNegInstruction());
      map.Add(42, InstructionFactory.BranchZeroInstruction());
      map.Add(43, InstructionFactory.HaltInstruction());
    }

    public Instruction this[int opcode]
    {
      get 
      {
        
        if(map.ContainsKey(opcode)) return map[opcode];

        //TODO Change ApplicationException to some Simpletron exception type.
        throw new ApplicationException(string.Format("{0} is not a valid opcode", opcode));
      }
    }
  }

  
}
